package com.itheima.dao;

import com.itheima.pojo.Order;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;

import java.util.List;
import java.util.Map;

public interface OrderDao {


    //判断是否重复预约（同一个人同一个时间预约了同一个套餐）
    @Select("select * from t_order where member_id=#{memberId} and orderDate=#{orderDate} and setmeal_id=#{setmealId}")
    Order findByCondition(Order order);

    //预约套餐详情
    @Insert("insert into t_order(id,member_id,orderDate,orderType,orderStatus,setmeal_id) values(#{id},#{memberId},#{orderDate},#{orderType},#{orderStatus},#{setmealId})")
    @SelectKey(statement = "select last_insert_id()", before = false, keyProperty = "id", resultType = Integer.class, keyColumn = "id")
    void add(Order order);

    //查询预约信息 Map
    @Select("select m.name member,s.name setmeal,o.orderDate orderDate,o.orderType orderType from t_order o,t_member m,t_setmeal s where o.id=#{id} and o.member_id=m.id and o.setmeal_id=s.id")
    Map findById(Integer id);


    //======================================================================================
    //今日预约数
    @Select("select count(orderStatus) from t_order where orderDate=#{today}")
    Integer findOrderCountByDate(String today);

    //今日到诊数
    @Select("select count(orderStatus) from t_order where orderDate=#{today} and orderStatus='已到诊'")
    Integer findVisitsCountByDate(String today);

    //本周预约人数
    @Select("select count(orderStatus) from t_order where orderDate >= #{thisWeekMonday}")
    Integer findOrderCountAfterDate(String thisWeekMonday);

    //本周到诊人数
    @Select("select count(orderStatus) from t_order where orderDate >= #{data} and orderStatus='已到诊'")
    Integer findVisitsCountAfterDate(String data);
    //热门套餐
    @Select("SELECT name,COUNT(o.id) setmeal_count,count(o.id)/(SELECT count(id) FROM t_order) proportion FROM t_order o,t_setmeal s WHERE s.id=o.setmeal_id GROUP BY o.setmeal_id ORDER BY setmeal_count DESC LIMIT 0,2")
    List<Map> findHotSetmeal();
}
